﻿using System.Linq;
using Transport.Core.Models.Parcels;
using Transport.Services.Infrastructure;

namespace Transport.Services.Parcels
{
    public class ParcelRepository : RepositoryBase<Parcel>, IParcelRepository
    {
        public ParcelRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        /// <summary>
        /// Search 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public IQueryable<Parcel> Search(string text)
        {
            return string.IsNullOrEmpty(text)
                                ? GetAll().Where(p => p.IsDeleted == false).OrderByDescending(p => p.ParcelId)
                                : GetAll()
                                .Where(p => p.IsDeleted == false && (
                                 p.Customer.CustomerName.Contains(text) || p.Receiver.Contains(text) || p.TrackingNumbers.Contains(text))).OrderByDescending(p => p.ParcelId);
        }


        public IQueryable<Parcel> Tracing(string[] trackingNumbers)
        {
            return GetAll().Where(p => p.IsDeleted == false && trackingNumbers.Contains(p.TrackingNumbers.Replace(" ", "")));
        }
    }
}
